package jjn.carl.array;

import java.util.Objects;

/**
 * @author Jiang Jining
 * @since 2023-06-29 23:26
 */
public class LeetCode209 {
    public int minSubArrayLen(int target, int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int min = Integer.MAX_VALUE;
        int sum = 0;
        int left = 0, right = 0;
        while (right < nums.length) {
            sum += nums[right];
            while (sum >= target) {
                min = Math.min(min, right - left + 1);
                sum = sum - nums[left];
                left++;
            }
            right++;
        }
        return Objects.equals(min, Integer.MAX_VALUE) ? 0 : min;
    }
    
    public static void main(String[] args) {
        int target = 7;
        int[] nums = new int[]{2, 3, 1, 2, 4, 3};
        int minSubArrayLen = new LeetCode209().minSubArrayLen(target, nums);
        System.out.println("minSubArrayLen = " + minSubArrayLen);
    }
}
